package cn.com.yusys.yusp.admin.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import cn.com.yusys.yusp.admin.domain.AdminSmBusiFunc;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmBusiFuncMapper;
import cn.com.yusys.yusp.commons.exception.MessageConstants;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;

/**
 * @version 1.0.0
 * @项目名称: yusp-admin
 * @类名称: AdminSysFunctionService
 * @类描述: 业务功能管理服务层
 * @功能描述:
 * @创建人: weimei1@yusys.com.cn
 * @创建时间: 2017-12-20 16:40
 * @修改备注:
 * @修改记录: 修改时间 修改人员 修改原因
 * -------------------------------------------------------------
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmBusiFuncService extends CommonService {

    @Autowired
    private AdminSmBusiFuncMapper adminSmBusiFuncMapper;

    @Override
    protected CommonMapper<?> getMapper() {
        return this.adminSmBusiFuncMapper;
    }

    /**
     * @方法名称: getFuncInfo
     * @方法描述: 业务功能信息查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getFuncInfo(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        if (model.getCondition().containsKey("funcName")) {
            if (model.getCondition().get("funcName") != null && !"".equals(model.getCondition().get("funcName"))) {
                model.getCondition().put("funcName", "%" + model.getCondition().get("funcName") + "%");
            }
        }
        if (model.getCondition().containsKey("funcUrl")) {
            if (model.getCondition().get("funcUrl") != null && !"".equals(model.getCondition().get("funcUrl"))) {
                model.getCondition().put("funcUrl", "%" + model.getCondition().get("funcUrl") + "%");
            }
        }
        List<Map<String, Object>> list = this.adminSmBusiFuncMapper.getFuncInfo(model);
        PageHelper.clearPage();
        return list;
    }

    /**
     * @方法名称: saveFunc
     * @方法描述: 新增业务功能信息
     * @参数与返回说明:
     * @算法描述:
     */
    public int createFunc(AdminSmBusiFunc adminSmBusiFunc) {
        adminSmBusiFunc.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.insertSelective(adminSmBusiFuncMapper, adminSmBusiFunc);
    }

    /**
     * @方法名称: editFunc
     * @方法描述: 修改业务功能信息
     * @参数与返回说明:
     * @算法描述:
     */
    public int editFunc(AdminSmBusiFunc adminSmBusiFunc) {
        adminSmBusiFunc.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.updateSelective(adminSmBusiFuncMapper, adminSmBusiFunc);
    }

    /**
     * @方法名称: deleteFunc
     * @方法描述: 删除业务功能，如果关联控制点和菜单则不能删除
     * @参数与返回说明: ids 数据ID主键
     * @算法描述:
     */
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public ResultDto<Object> deleteFunc(String[] idStr) {
        int n = 0;
        List<Object> i18nData = new ArrayList<Object>();
        ResultDto<Object> resultDto = new ResultDto<Object>();
        if (idStr != null && !"".equals(idStr.toString())) {
            int undeletes = 0;
            int undeletes1 = 0;
            for (int i = 0; i < idStr.length; i++) {
                if (!"".equals(idStr[i])) {
                    int contrNum = this.adminSmBusiFuncMapper.getContrByFuncId(idStr[i]);
                    int menuNum = this.adminSmBusiFuncMapper.getMenuByFuncId(idStr[i]);
                    if (contrNum > 0) {
                        undeletes++;
                    } else if (menuNum > 0) {
                        undeletes1++;
                    } else {
                        n = n + adminSmBusiFuncMapper.deleteByPrimaryKey(idStr[i]);
                    }
                }
            }
            if (undeletes == 0 && undeletes1 == 0) {
//                result.put("message", "成功删除" + n + "条数据!");
            	resultDto.setCode(0);
            	resultDto.setMessage("成功删除" + n + "条数据!");
            	i18nData.add(n);
            } else if (undeletes == 0 && undeletes1 != 0) {
//                result.put("message", "成功删除" + n + "条数据,有" + undeletes1 + "条数据由于关联菜单，无法删除!");
                resultDto.setCode(0);
            	resultDto.setMessage("成功删除" + n + "条数据,有" + undeletes1 + "条数据由于关联菜单，无法删除!");
            	i18nData.add(n);
            	i18nData.add(undeletes1);
            } else if (undeletes != 0 && undeletes1 == 0) {
//                result.put("message", "成功删除" + n + "条数据,有" + undeletes + "条数据由于关联控制点，无法删除!");
                resultDto.setCode(0);
            	resultDto.setMessage("成功删除" + n + "条数据,有" + undeletes + "条数据由于关联控制点，无法删除!");
            	i18nData.add(n);
            	i18nData.add(undeletes);
            } else {
//                result.put("message", "成功删除" + n + "条数据,有" + (undeletes + undeletes1) + "条数据由于关联菜单和控制点，无法删除!");
                resultDto.setCode(0);
            	resultDto.setMessage("成功删除" + n + "条数据,有" + (undeletes + undeletes1) + "条数据由于关联菜单和控制点，无法删除!");
            	i18nData.add(n);
            	i18nData.add(undeletes + undeletes1);
            }
        }
        resultDto.setI18nData(i18nData);
        return resultDto;
    }
}
